# -*- coding:utf-8 -*-

# @Time     :2024/01/19/16/
# @Author   : Ann 8194486
# Project   : tj_automation_test
# import win32gui

import time
import os
import allure
import urllib.parse
from allure_commons.types import AttachmentType
from selenium.common import NoSuchWindowException
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from tools.reader.parser_reader import secret_reader
from tools.public.decorators import ui_exception_record, add_watermark
from tools.public.element_locator import ElementsLookUp
# from tools.secret_utils.secret_selenium import decrypt_aes
from tools.reader.config_reader import ConfigReader


class SeleniumWidget(object):

    def __init__(self, driver=None):
        self._flag = {}
        self._fetch_dict = {}
        self._location_type_dict = {
            "id": By.ID,
            "name": By.NAME,
            "xpath": By.XPATH,
            "css_selector": By.CSS_SELECTOR,
            "class_name": By.CLASS_NAME,
            "tag_name": By.TAG_NAME,
            "link_text": By.LINK_TEXT
        }
        if driver:
            self._driver = driver
        else:
            raise TimeoutError("No Match Driver!")

    @ui_exception_record
    def _open(self, link):
        """
        Open the webpage link
        :param link:
        """
        self._driver.get(link)

    def _open_blank_window(self):
        """
        open new blank window
        """
        new_window = f"window.open('about:blank')"
        self._driver.execute_script(new_window)
        self._switch_to_new_window_for_selenium()

    @ui_exception_record
    def _open_new_window_named_as(self, window_name):
        """
        Open a new webpage with the specified name
        :param window_name:
        """

        def isEmpty(var):
            if len(var) == 0:
                return True

        try:
            if window_name is not None and not isEmpty(window_name):
                self._open_blank_window()
        except NoSuchWindowException as e:
            raise e

    @ui_exception_record
    def _open_new_window(self, link):
        """
        open new window
        """
        # 对链接进行URL编码，防止注入攻击
        safe_link = urllib.parse.quote(link)
        js = f"window.open('{safe_link}')"
        self._driver.execute_script(js)
        # 获取所有窗口句柄
        handles = self._driver.window_handles
        # 切换到新打开的窗口
        self._driver.switch_to.window(handles[-1])

    @ui_exception_record
    def _close_browser(self):
        """
        close browser
        :return:
        """
        self._driver.quit()

    @ui_exception_record
    def _close_window_named_as(self, window_name):
        """
        关闭指定名称页面
        :param window_name:
        :return:
        """
        # 获取所有窗口的句柄
        window_handles = self._driver.window_handles
        # 遍历所有窗口句柄，找到目标窗口并关闭它
        for handle in window_handles:
            # 跳转到遍历的页面去获取窗口名称
            self._driver.switch_to.window(handle)
            # 判断窗口名称是否一致
            if self._driver.title == window_name:
                self._driver.close()
                self._driver.switch_to.window(handle[0])
                break

    @ui_exception_record
    def _refresh(self):
        """
        刷新当前页面
        :return:
        """
        self._driver.refresh()

    @ui_exception_record
    def _back(self):
        """
        模拟浏览器后退操作
        :return:
        """
        self._driver.back()

    @ui_exception_record
    def _forward(self):
        """
        模拟浏览器前进操作
        :return:
        """
        self._driver.forward()

    @ui_exception_record
    def _switch_to_base_window_for_selenium(self):
        """
        跳转到基础页面
        :return:
        """
        window_handles = self._driver.window_handles
        main_handle = window_handles[0]
        self._driver.switch_to.window(main_handle)

    @ui_exception_record
    def _switch_to_new_window_for_selenium(self):
        """
        跳转到当前页面后一个页面
        :return:
        """
        try:
            # 轮询窗口句柄列表，直到找到一个与当前窗口不同的窗口
            while True:
                window_handles = self._driver.window_handles
                current_handle = self._driver.current_window_handle
                if current_handle not in window_handles:
                    break
                new_window_handle = window_handles.index(current_handle) + 1
                self._driver.switch_to.window(window_handles[new_window_handle])
        except NoSuchWindowException:
            print("No next window found within the specified time.")
            return None
        except Exception as e:
            print(f"An error occurred: {e}")
            return None

    @ui_exception_record
    def _switch_to_window_named_as_for_selenium(self, window_name):
        """
        跳转到指定名称页面
        :param window_name:
        :return:
        """
        window_handles = self._driver.window_handles
        # 遍历所有窗口句柄，找到目标窗口并关闭它
        for handle in window_handles:
            self._driver.switch_to.window(handle)
            if self._driver.title == window_name:
                break

    @ui_exception_record
    def _switch_to_window_where_url_is_for_selenium(self, url):
        """
        跳转到指定url页面
        :param url:
        :return:
        """
        window_handles = self._driver.window_handles
        for handle in window_handles:
            self._driver.switch_to.window(handle)
            if self._driver.current_rul == url:
                break

    @ui_exception_record
    def _switch_to_root_frame_for_selenium(self):
        """
        跳转到最外层框架
        :return:
        """
        self._driver.switch_to.default_content()

    @ui_exception_record
    def _switch_to_frame_with_name_for_selenium(self, frame_name):
        """
        跳转到指定名称的框架内
        :param frame_name:
        :return:
        """
        # 获取所有frame的id或name
        frames = self._driver.find_elements(By.TAG_NAME, 'iframe') + self._driver.find_elements(By.NAME)
        # 遍历所有frame，找到目标frame并切换到它
        for frame in frames:
            if frame_name in frame.get_attribute('id') or frame_name == frame.get_attribute('name'):
                self._driver.switch_to.frame(frame)
                break

    @ui_exception_record
    def _switch_to_frame_for_selenium(self, element_locator):
        """
        跳转到指定元素的框架内
        :param element_locator:
        :return:
        """
        target_frame = ElementsLookUp.element_lookup(self._driver, element_locator)
        self._driver.switch_to.frame(target_frame)

    @ui_exception_record
    def _just_wait_in(self, time_value):
        """
        设置显性等待时间 单位seconds
        :param time_value:
        :return:
        """
        time.sleep(time_value)

    # @ui_exception_record
    # def _wait_for_window_to_close(self, window_title):
    #     while True:
    #         # 查找窗口句柄
    #         hwnd = win32gui.FindWindow(None, window_title)
    #
    #         # 如果窗口句柄存在，说明窗口仍然打开
    #         if hwnd != 0:
    #             time.sleep(1)  # 等待一秒后继续检查
    #         else:
    #             # 如果窗口句柄不存在，说明窗口已经关闭，跳出循环
    #             break

    @ui_exception_record
    def _wait_new_window_popup_then_switch_to_new_window_for_selenium(self, time_value):
        """
        等待新窗口打开并跳转
        :param time_value:
        :return:
        """
        try:
            # 轮询窗口句柄列表，直到找到一个与当前窗口不同的窗口
            while True:
                window_handles = self._driver.window_handles
                current_handle = self._driver.current_window_handle
                if current_handle not in window_handles:
                    WebDriverWait(self._driver, time_value).until(current_handle)
                    break
                new_window_handle = window_handles.index(current_handle) + 1
                self._driver.switch_to.window(window_handles[new_window_handle])
        except NoSuchWindowException:
            print(f"New window not open within {time_value} seconds or failed to switch.")
            return None
        except Exception as e:
            print(f"An error occurred: {e}")
            return None

    @ui_exception_record
    def _get_screenshot_for_selenium(self):
        """
        截取当前页面的全屏截图
        :return:
        """
        time.sleep(0.5)
        # 定义图片存放位置
        directory_path = ConfigReader().read("screenshot")["classpath"]
        # 判断文件夹位置是否存在
        is_exist = os.path.exists(directory_path)
        if not is_exist:
            os.makedirs(directory_path)
        # 定义时间格式
        now_time = time.strftime("%Y.%m.%d.%H.%M.%S")
        file_name = "%s.png" % now_time
        # 以时间定义图片的名称
        full_path = f"{directory_path}/{file_name}"
        self._driver.save_screenshot(full_path)
        # 添加水印
        add_watermark(full_path)
        allure.attach.file(full_path, file_name, attachment_type=AttachmentType.PNG)

    @ui_exception_record
    def _get_screenshot_of_element_for_selenium(self, element_locator):
        """
        截取某个具体元素的截图
        :param element_locator:
        :return:
        """
        time.sleep(0.5)
        # 定义图片存放位置
        directory_path = ConfigReader().read("screenshot")["classpath"]
        # 判断文件夹位置是否存在
        is_exist = os.path.exists(directory_path)
        if not is_exist:
            os.makedirs(directory_path)
        # 定义时间格式
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        full_path = directory_path + "/" + element_locator + ".png"
        element.screenshot(full_path)
        add_watermark(full_path)
        allure.attach.file(full_path, element_locator, attachment_type=AttachmentType.PNG)

    @ui_exception_record
    def _assert_that_window_title_is_for_selenium(self, title, time_until):
        """
        判断窗口标题是否是期望的标题
        :param title:
        :param time_until:
        :return:
        """
        WebDriverWait(self._driver, time_until).until(EC.title_is(title))

    @ui_exception_record
    def _assert_that_window_title_contains_for_selenium(self, content, time_until):
        """
        判断窗口标题是否包含期望的内容
        :param content:
        :param time_until:
        :return:
        """
        WebDriverWait(self._driver, time_until).until(EC.title_contains(content))

    @ui_exception_record
    def _assert_that_current_url_is_for_selenium(self, url, time_until):
        """
        判断窗口url是否是期望的url
        :param url:
        :param time_until:
        :return:
        """
        WebDriverWait(self._driver, time_until).until(EC.url_to_be(url))

    @ui_exception_record
    def _assert_that_current_url_contains_for_selenium(self, content, time_until):
        """
        判断窗口url是否包含期望的url
        :param content:
        :param time_until:
        :return:
        """
        WebDriverWait(self._driver, time_until).until(EC.url_contains(content))

    @ui_exception_record
    def _assert_that_current_url_matches_for_selenium(self, regex, time_until):
        """
        判断窗口url是否包含符合正则表达式的内容
        :param regex:
        :param time_until:
        :return:
        """
        WebDriverWait(self._driver, time_until).until(EC.url_matches(regex))

    @ui_exception_record
    def _click(self, element_locator):
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        element.click()

    @ui_exception_record
    def _click_which_time_out_in(self, element_locator, time_value):
        """
        判断元素是否可点击，可点击的话点击
        :param element_locator:
        :param time_value:
        :return:
        """

        locator_type = ElementsLookUp.get_element_details(element_locator)[0]
        locator_expression = ElementsLookUp.get_element_details(element_locator)[1]
        if locator_type in self._location_type_dict:
            if WebDriverWait(self._driver, time_value).until(
                    EC.element_to_be_clickable((self._location_type_dict[locator_type], locator_expression))):
                element = ElementsLookUp.element_lookup(self._driver, element_locator)
                element.click()
            else:
                raise Exception("element not clickable")
        else:
            raise TypeError(
                "The positioning method is abnormal, plz check whether the positioning method is correct")

    @ui_exception_record
    def _click_by_js_which_time_out_in(self, element_locator, time_value):
        """
        判断元素是否可点击，可点击的话点击
        :param element_locator:
        :param time_value:
        :return:
        """

        locator_type = ElementsLookUp.get_element_details(element_locator)[0]
        locator_expression = ElementsLookUp.get_element_details(element_locator)[1]
        if locator_type in self._location_type_dict:
            if WebDriverWait(self._driver, time_value).until(
                    EC.element_to_be_clickable((self._location_type_dict[locator_type], locator_expression))):
                ActionChains(self._driver).click(ElementsLookUp.element_lookup(self._driver, element_locator)).perform()
            else:
                raise Exception("element not clickable")
        else:
            raise TypeError(
                "The positioning method is abnormal, plz check whether the positioning method is correct")

    @ui_exception_record
    def _assert_element_is_visible_for_selenium(self, element_locator, time_until):
        """
        确定元素是否可见，如果可见则返回元素
        :param element_locator:
        :param time_until:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        WebDriverWait(self._driver, time_until).until(EC.visibility_of(element))

    @ui_exception_record
    def _assert_element_value_equals_expect_value_for_selenium(self, element_locator, expect_value):
        """
        确定元素值是否与预期值相同
        :param element_locator:
        :param expect_value:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        data_value = element.get_attribute("value")
        if data_value != expect_value:
            with allure.step(
                    f"Element: '{element_locator}', Real Value: '{data_value}', Except Value: '{expect_value}'."):
                assert False

    @ui_exception_record
    def _assert_element_text_equals_expect_value_for_selenium(self, element_locator, expect_value):
        """
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        data_text = element.text
        if data_text != expect_value:
            with allure.step(
                    f"Element: '{element_locator}', Real Value: '{data_text}', Except Value: '{expect_value}'."):
                assert False

    @ui_exception_record
    def _initial_key_flag_default_for_selenium(self):
        flag_key = None
        self._flag.setdefault("Flag", flag_key)

    @ui_exception_record
    def _check_key_flag_default_for_selenium(self):
        flag_key = self._flag["Flag"]
        if flag_key == "False":
            assert False

    @ui_exception_record
    def _assert_element_text_equals_expect_value_with_flag(self, element_locator, expect_value):
        """
        确定元素值是否与预期值相同并保存flag继续执行脚本
        :param element_locator:
        :param expect_value:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        data_text = element.text
        if data_text != expect_value:
            with allure.step(
                    f"Element: '{element_locator}',Real Value: '{data_text}', Except Value: '{expect_value}'."):
                self._flag.update({"Flag": "False"})

    @ui_exception_record
    def _assert_element_value_equals_expect_value_with_flag(self, element_locator, expect_value):
        """
        确定元素值是否与预期值相同并保存flag继续执行脚本
        :param element_locator:
        :param expect_value:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        data_value = element.get_attribute("value")
        if data_value != expect_value:
            with allure.step(
                    f"Element: '{element_locator}', Real Value: '{data_value}', Except Value: '{expect_value}'."):
                self._flag.update({"Flag": "False"})

    @ui_exception_record
    def _search_elements_for_value_then_click_for_selenium(self, element_locator, value):
        """
        在页面上查找某元素的值是否是期望值然后点击
        :param element_locator:
        :param value:
        :return:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        element_value = element.get_attribute("value")
        if element_value == value:
            element.click()

    @ui_exception_record
    def _search_elements_for_text_then_click_for_selenium(self, element_locator, value):
        """
        在页面上查找某元素的值是否是期望值然后点击
        :param element_locator:
        :param value:
        :return:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        element_text = element.text
        if element_text == value:
            element.click()

    @ui_exception_record
    def _search_elements_for_value_then_click_by_js_for_selenium(self, element_locator, value):
        """
        在页面上查找某元素的值是否是期望值然后使用Js点击
        :param element_locator:
        :param value:
        :return:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        element_value = element.get_attribute("value")
        if element_value == value:
            ActionChains(self._driver).click(ElementsLookUp.element_lookup(self._driver, element_locator)).perform()

    @ui_exception_record
    def _search_elements_for_text_then_click_by_js_for_selenium(self, element_locator, value):
        """
        在页面上查找某元素的值是否是期望值然后使用Js点击
        :param element_locator:
        :param value:
        :return:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        element_text = element.text
        if element_text == value:
            ActionChains(self._driver).click(ElementsLookUp.element_lookup(self._driver, element_locator)).perform()

    @ui_exception_record
    def _double_click(self, element_locator):
        """
        Double-click
        """
        ActionChains(self._driver).double_click(
            ElementsLookUp.element_lookup(self._driver, element_locator)).perform()

    @ui_exception_record
    def _context_click(self, element_locator):
        """
        Right-click
        """
        ActionChains(self._driver).context_click(
            ElementsLookUp.element_lookup(self._driver, element_locator)).perform()

    @ui_exception_record
    def _wait_for_element_appear_for_selenium(self, element_locator, time_value):
        """
        Waiting for element appear in page
        :param element_locator:
        """
        locator_type = ElementsLookUp.get_element_details(element_locator)[0]
        locator_expression = ElementsLookUp.get_element_details(element_locator)[1]
        if locator_type in self._location_type_dict:
            WebDriverWait(self._driver, time_value).until(
                EC.presence_of_element_located((self._location_type_dict[locator_type], locator_expression)))

    @ui_exception_record
    def _scroll_element_into_view_for_selenium(self, element_locator):
        """
        滚动滚动条到对应元素
        :param element_locator:
        :return:
        """
        # element = ElementsLookUp.element_lookup(self._driver, element_locator)
        # location = element.location
        # middle = location['y'] / 2
        # half_height = location['y'] / 2
        # target_position = location['y'] - middle + half_height
        # # 滚动到目标位置
        # self._driver.execute_script("window.scrollTo(0, %s)" % target_position)
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        self._driver.execute_script("arguments[0].scrollIntoView()", element)

    @ui_exception_record
    def _scroll_to_current_window_top_for_selenium(self):
        """
        滚动到当前页面顶部
        :return:
        """
        self._driver.execute_script("window.scrollTo(0, 0)")

    @ui_exception_record
    def _scroll_to_current_window_bottom_for_selenium(self):
        """
        滚动到当前页面底部
        :return:
        """
        self._driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")

    @ui_exception_record
    def _clear(self, element_locator):
        """
        清除输入框中的内容
        :return:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        element.clear()

    @ui_exception_record
    def _type_value_into_element_for_selenium(self, data_value, element_locator):
        """
        将数据输入指定元素中
        :param data_value:
        :param element_locator:
        :return:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        element.send_keys(data_value)

    @ui_exception_record
    def _type_value_into_element_by_js_for_selenium(self, data_value, element_locator):
        """
        使用js将数据输入指定元素中
        :param data_value:
        :param element_locator:
        :return:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)

        ActionChains.send_keys_to_element(element, data_value)

    @ui_exception_record
    def _clear_and_type_value_into_element_for_selenium(self, data_value, element_locator):
        """
        将文本框数据清空并输入新的数据
        :param data_value:
        :param element_locator:
        :return:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        element.clear()
        element.send_keys(data_value)

    @ui_exception_record
    def _fetch_text_as_key_for_selenium(self, element_locator, set_key):
        """
        获取元素的文本属性并赋予Key值保存
        :param element_locator:
        :param set_key:
        :return:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        data_value = element.text
        self._fetch_dict.setdefault(set_key, data_value)

    @ui_exception_record
    def _fetch_value_as_key_for_selenium(self, element_locator, set_key):
        """
        获取元素的文本属性并赋予Key值保存
        :param element_locator:
        :param set_key:
        :return:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        data_value = element.get_attribute("value")
        self._fetch_dict.setdefault(set_key, data_value)

    @ui_exception_record
    def _fetch_element_attribute_as_key_for_selenium(self, element_locator, attribute, set_key):
        """
        获取元素的属性并赋予key值保存
        :param element_locator:
        :param attribute:
        :param set_key:
        :return:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        data_value = element.get_attribute(attribute)
        self._fetch_dict.setdefault(set_key, data_value)

    @ui_exception_record
    def _type_key_into_element_for_selenium(self, set_key, element_locator):
        """
        将对应key值保存的数据键入元素
        :param set_key:
        :param element_locator:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        data_value = self._fetch_dict[set_key]
        element.send_keys(data_value)

    @ui_exception_record
    def _click_and_hold_for_selenium(self, element_locator):
        """
        点击并保持
        :param element_locator:
        :return:
        """
        ActionChains(self._driver).click_and_hold(
            ElementsLookUp.element_lookup(self._driver, element_locator)).perform()

    @ui_exception_record
    def _type_username_for_role_into_element_for_selenium(self, role, element_locator):
        """
        将对应role的用户名键入
        :param role:
        :param element_locator:
        :return:
        """
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        secret_dict = secret_reader()
        for i in secret_dict:
            if i["role"] == role:
                element.send_keys(i["username"])

    # @ui_exception_record
    # def _type_password_for_role_into_element_for_selenium(self, role, element_locator):
    #     """
    #     将对应role的密码键入
    #     :param role:
    #     :param element_locator:
    #     :return:
    #     """
    #     element = ElementsLookUp.element_lookup(self._driver, element_locator)
    #     secret_dict = secret_reader()
    #     for i in secret_dict:
    #         if i["role"] == role:
    #             element.send_keys(decrypt_aes(i["password"], i["key"]))

    # @ui_exception_record
    # def _type_password_for_key_into_element_for_selenium(self, cipher_text, aes_key, element_locator):
    #     """
    #     Use the Key value and encryption fields to type the password
    #     :param cipher_text:
    #     :param aes_key:
    #     :param element_locator:
    #     """
    #     element = ElementsLookUp.element_lookup(self._driver, element_locator)
    #     password = decrypt_aes(cipher_text, aes_key)
    #     element.send_keys(password)

    @ui_exception_record
    def _drag_and_drop_for_selenium(self, source_element, target_element):
        """
        在源元素上按住鼠标左键，然后移动到目标元素并释放鼠标按钮。
        """
        ActionChains(self._driver).drag_and_drop(source_element, target_element)

    @ui_exception_record
    def _drag_and_drop_by_offset_for_selenium(self, source_element, xoffset, yoffset):
        """
        点击元素并拖动到坐标位置
        :param source_element:  The element to mouse down.
        :param xoffset: The abscissa position of the element
        :param yoffset: The ordinate position of the element
        """
        ActionChains(self._driver).drag_and_drop_by_offset(source_element, xoffset, yoffset)

    @ui_exception_record
    def _select_option_from_dropdownlist(self, element_locator1, element_locator2):
        """
        选择下拉框选项
        :param element_locator1:
        :param element_locator2:
        :return:
        """
        dropdown_list = ElementsLookUp.element_lookup(self._driver, element_locator2)
        dropdown_list.click()
        self._wait_for_element_appear_for_selenium(element_locator1, 3)
        option = ElementsLookUp.element_lookup(self._driver, element_locator1)
        option.click()


    @ui_exception_record
    def _press_key_on(self, key_name, element_locator):
        # key_to_press = getattr(Keys, key_name, None)
        # if key_to_press is None:
        #     raise ValueError(f"Invalid key name: {key_name}")
        # js_code = f"var event = new KeyboardEvent('keydown',{{'key':'{key_name.upper()}'}}); document.dispatchEvent(event);"
        # self._driver.execute_script(js_code)
        KEY_MAPPING = {
            'ENTER': Keys.ENTER,
            'TAB': Keys.TAB,
            'ESCAPE': Keys.ESCAPE,
            'BACKSPACE': Keys.BACKSPACE,
            'BACK_SPACE': Keys.BACKSPACE,
            'CLEAR': Keys.CLEAR,
            'RETURN': Keys.RETURN,
            'SHIFT': Keys.SHIFT,
            'LEFT_SHIFT': Keys.SHIFT,
            'CONTROL': Keys.CONTROL,
            'LEFT_CONTROL': Keys.CONTROL,
            'ALT': Keys.ALT,
            'LEFT_ALT': Keys.ALT,
            'PAUSE': Keys.PAUSE,
            'SPACE': Keys.SPACE,
            'PAGE_UP': Keys.PAGE_UP,
            'PAGE_DOWN': Keys.PAGE_DOWN,
            'END': Keys.END,
            'HOME': Keys.HOME,
            'LEFT': Keys.LEFT,
            'ARROW_LEFT': Keys.ARROW_LEFT,
            'UP': Keys.UP,
            'ARROW_UP': Keys.ARROW_UP,
            'RIGHT': Keys.RIGHT,
            'ARROW_RIGHT': Keys.ARROW_RIGHT,
            'DOWN': Keys.DOWN,
            'ARROW_DOWN': Keys.ARROW_DOWN,
            'INSERT': Keys.INSERT,
            'DELETE': Keys.DELETE,
            'SEMICOLON': Keys.SEMICOLON,
            'EQUALS': Keys.EQUALS,
            'NUMPAD0': Keys.NUMPAD0,
            'NUMPAD1': Keys.NUMPAD1,
            'NUMPAD2': Keys.NUMPAD2,
            'NUMPAD3': Keys.NUMPAD3,
            'NUMPAD4': Keys.NUMPAD4,
            'NUMPAD5': Keys.NUMPAD5,
            'NUMPAD6': Keys.NUMPAD6,
            'NUMPAD7': Keys.NUMPAD7,
            'NUMPAD8': Keys.NUMPAD8,
            'NUMPAD9': Keys.NUMPAD9,
            'MULTIPLY': Keys.MULTIPLY,
            'ADD': Keys.ADD,
            'SEPARATOR': Keys.SEPARATOR,
            'SUBTRACT': Keys.SUBTRACT,
            'DECIMAL': Keys.DECIMAL,
            'DIVIDE': Keys.DIVIDE,
            'F1': Keys.F1,
            'F2': Keys.F2,
            'F3': Keys.F3,
            'F4': Keys.F4,
            'F5': Keys.F5,
            'F6': Keys.F6,
            'F7': Keys.F7,
            'F8': Keys.F8,
            'F9': Keys.F9,
            'F10': Keys.F10,
            'F11': Keys.F11,
            'F12': Keys.F12,
            'META': Keys.META,
            'COMMAND': Keys.COMMAND,
            'ZENKAKU_HANKAKU': Keys.ZENKAKU_HANKAKU,
        }
        key_to_send = KEY_MAPPING[key_name]
        element = ElementsLookUp.element_lookup(self._driver, element_locator)
        element.send_keys(key_to_send)


    # @ui_exception_record
    # def wait_element_appear_which_times_out_in(self,element_locator,time_value):
    #     while True:
    #         try:
    #             element = ElementsLookUp.element_lookup(self._driver, element_locator)
    #
    #         except:

